package com.desktopmng.orm;

import java.util.Date;
import java.util.Calendar;

public class CalendarViewFormat {

	public CalendarViewFormat(String viewtype, Date date, int weekStartDay) {
		int index, w;
		Calendar c = Calendar.getInstance();
		CalendarViewFormat.Type type = CalendarViewFormat.Type.valueOf(viewtype);
		switch (type) {
			case day:
				c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 00, 00, 00);
				setStartDate(c.getTime());
				c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
				setEndDate(c.getTime());
				break;
			case week:
				index = viewto(weekStartDay);
				c.setTime(date);
                w = index - 7;
                c.add(Calendar.DAY_OF_WEEK, w);
                c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 00, 00, 00);
                setStartDate(c.getTime());
                c.setTime(date);
                w = 7 - c.get(Calendar.DAY_OF_WEEK);
                c.add(Calendar.DAY_OF_WEEK, w);
                c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
                setEndDate(c.getTime());
                break;
			case month:
				c.setTime(date);
				c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 00, 00, 00);
                c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 00, 00, 00);
                setStartDate(c.getTime());
                c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
                setEndDate(c.getTime());
		}
	}
	
	private int viewto(int weekStartDay) {
		int index = 0;
		switch (weekStartDay) {
			case 7:
				index = 1;
			case 6:
				index = 7;
			case 5:
				index = 6;
			case 4:
				index = 5;
			case 3:
				index = 4;
			case 2:
				index = 3;
			case 1:
				index = 2;
		}
		return index;
	}
	
	private Date startDate;
	
	private Date endDate;

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public enum Type {
		day, week, workweek, month,
	}
}
